package com.cqupt.filter;

import com.cqupt.pojo.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginVerificationFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession();

        String uri = request.getRequestURI();
        // 判断请求的url
        String requestPage = uri.substring(uri.lastIndexOf("/") + 1);
        // 如果请求的是login页面，放行
        if(requestPage.equals("login.html") || requestPage.equals("screen.html")) {
            chain.doFilter(request, response);
        } else {
            User loginUser = (User)session.getAttribute("loginUser");
            if(loginUser == null) {
//                response.sendRedirect(request.getContextPath() + "/login.html");
            } else {
                chain.doFilter(request, response);
            }
        }

    }

    @Override
    public void destroy() {

    }
}
